var express = require('express');
var router = express.Router();
const infoSchema = require('../module/userinfo');
const jwt = require('jsonwebtoken');
let secret = 'houduan'


router.post('/getinfo', function (req, res, next) {
    let { token } = req.body
    try {
        let data = jwt.verify(token, secret)
        let phone = data.username
        infoSchema.find({ phone }, (err, doc) => {
            if (err) throw err
            res.json({
                status: 0,
                msg: "获取成功",
                data: doc[0]
            })
        })

    } catch (error) {
        res.json({
            status:4,
            msg: "token过期，请重新登入"
        })
    }
});
//修改个人信息
router.post('/changeinfo', function (req, res, next) {
    let { token, username, photo, sex, year, month, day, city } = req.body
    try {
        let data = jwt.verify(token, secret)
        let phone = data.username

        infoSchema.find({ username }, (err, doc) => {
            if (err) throw err
            if (doc.length) {
                res.json({
                    status:1,
                    msg: "用户名重复"
                })
            } else {
                infoSchema.update({ phone }, { username, photo, sex, year, month, day, city, phone }, (err, doc) => {
                    if (err) throw err
                    res.json({
                        status: 0,
                        msg: "修改成功"
                    })
                })
            }
        })

    } catch (error) {
        res.json({
            status:4,
            msg: "token过期，请重新登入"
        })
    }
});



module.exports = router;